import { getuserinfo } from '@/api/user'
import { rendermenu } from '@/util/rendermenu'
import Vue from 'vue'
import Vuex from 'vuex'
import router from "../router/index"
Vue.use(Vuex)

const routesdiyc = {
    "/user": () => import(/* webpackChunkName: "user" */ '../views/user/index.vue'),
    "/order/add": () => import(/* webpackChunkName: "add" */ '../views/order/add.vue'),
    "/order/list": () => import(/* webpackChunkName: "list" */ '../views/order/list.vue'),
    "/order": () => import(/* webpackChunkName: "order" */ '../views/order/index.vue'),
    "/myself": () => import(/* webpackChunkName: "myself" */ '../views/order/myself.vue'),
}
console.log("打包的路由", routesdiyc);

const store = new Vuex.Store({
    state: {
        menulist: [
            // {
            //     title: '用户管理',
            //     index: '1',
            //     icon: 'el-icon-location',
            //     children: [
            //         {
            //             title: '用户列表',
            //             index: '/user',
            //             icon: 'el-icon-location',
            //             children: [],
            //         },
            //         // {
            //         //   title: '用户列表2',
            //         //   index: 'user2',
            //         //   icon: 'el-icon-location',
            //         //   children: [],
            //         // },
            //     ],
            // },
            // {
            //     title: '订单管理',
            //     index: '2',
            //     icon: 'el-icon-location',
            //     children: [
            //         {
            //             title: '发布订单',
            //             index: '/order/add',
            //             icon: 'el-icon-location',
            //             children: [],
            //         },
            //         {
            //             title: '个人中心',
            //             index: '/myself',
            //             icon: 'el-icon-location',
            //             children: [],
            //         },
            //         {
            //             title: '订单列表',
            //             index: '/order/list',
            //             icon: 'el-icon-location',
            //             children: [],
            //         },
            //         {
            //             title: '接单大厅',
            //             index: '/order',
            //             icon: 'el-icon-location',
            //             children: [],
            //         },
            //     ],
            // },
            // {
            //     title: '系统设置',
            //     index: '3',
            //     icon: 'el-icon-location',
            //     children: [],
            // },
            // {
            //     title: '外观设置',
            //     index: '6',
            //     icon: 'el-icon-location',
            //     children: [],
            // },
        ],
        count: 0,
        isCollapse: false,
        list: [
            { type: 1 },
            { type: 1 },
            { type: 2 },
            { type: 2 },
            { type: 3 },
            { type: 3 },
        ],
        userinfo: {
            name: "刘华"
            // Vue实例.$store.state.userinfo.name
        }
    },
    getters: {
        getlist(state) {
            return state.list.filter(v => v.type < 3)
        }
    },
    mutations: {
        CHANGE_MENU(state, val) {
            state.menulist = val
        },

        //  社区规范 名称统一大写
        CHANGE_NAME(state, val) {
            console.log("触发了mutations", state, val);
            state.userinfo.name = val
        },
        increment(state) {
            state.count++
        },
        //  修改isCollapse 
        CHANGE_Collapse(state, val) {
            state.isCollapse = val
        }
    },
    actions: {
        async getmens(store) {
            let result = await getuserinfo()
            // 发生请求更新菜单 
            console.log("菜单", result);
            //     对菜单进行出路
            let list = rendermenu(result.data)
            console.log(list);
            store.commit("CHANGE_MENU", list)
            // 获得菜单以后, 顺便添加路由

            // 验证是否能用打代码来添加路由
            // router.addRoute("home", {
            //     path: "/myself", name: "myself",
            //     component: () => import(/* webpackChunkName: "myself" */ '../views/order/myself.vue')
            // })
            // 打包工具的原理
            // 确保所有 的文件都会被打包
            //  指定某个目录中的文件 全部被打包 
            // console.log("myself", myself);
            result.data.forEach(v => {
                if (v.view) {
                    console.log("加载的对象", routesdiyc[v.path]);
                    router.addRoute("home", {
                        ...v,
                        //                           
                        component: routesdiyc[v.path]
                    })
                }
            })



        }
    }
})


export default store